package com.mayjean.db.config;

import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
import com.mayjean.db.tenant.ITenantId;
import com.mayjean.db.tenant.NezhaTenantHandler;
import com.mayjean.db.tenant.NezhaTenantProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Tenant configuration
 *
 * @author kyleju
 */
@Configuration
@EnableConfigurationProperties(NezhaTenantProperties.class)
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "nezha.starters",value = "tenant",havingValue = "true")
public class TenantConfiguration {

	private final NezhaTenantProperties tenantProperties;
	private final ObjectProvider<ITenantId> iTenantIdObjectProvider;

	@Bean
	@ConditionalOnMissingBean(TenantHandler.class)
	public TenantHandler tenantHandler() {
		return new NezhaTenantHandler(tenantProperties, iTenantIdObjectProvider.getIfAvailable());
	}
}
